/**
 * 万年历看板 淡旺季数据 漏斗图
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/funnel');

    var myChart = ec.init(document.getElementById('seasonFunnel_EC_m_month'));
 
   var option_day = {
    title : {
        x: 'left',
        y: 'bottom'
    },
    tooltip : {
        trigger: 'item',
        formatter: tooltipFunc
    },
    toolbox: {
        show : true,
        orient: 'vertical',
        y: 'center',
        feature : {
            mark : {show: true},
            dataView : {show: true, readOnly: false},
            magicType : {
                show: true, 
                type: ['pie', 'funnel'],
                option: {
                    pie: {
                        radius : [0, 50]
                    }
                }
            },
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    legend: {
        orient: 'vertical',
        x: 'left',
        data : []
    },
    calculable : true,
    series : [
        
        {
            name:'漏斗图',
            type:'funnel',
            width: '10%',
            height: '45%',
            x:'25%',
            y: '10%',
            funnelAlign: 'left',
            
            center: ['25%', '25%'],  // for pie
            
            data:[]
        }
    ]
};

 var comment='';
 
    function show(infoTrend,eventType){
        var source_option = $('#funnel_m_month').val();
		var platform ='m';
		var month_num =$('#month_option_m').val();
		var season_Type =$('#season_m_month').val();

				
        myChart.showLoading({
            text: '正在努力的读取数据中...'
        });
        $.ajax({
            url: '/dashboard/calendar/ajax/getFunnelMonth',
            type: 'post',
            async: true,    
            data:{"businessName":businessName, "month_num":month_num,"season_Type":season_Type,"source_option":source_option,"platform":platform},
            dataType: 'json',
            timeout:5000,
            success: function(data, textStatus) {
               
                option_day.legend.data = data.legend;
                option_day.series[0].data = data.datalist;
                comment=data.comment;
                
                myChart.setOption(option_day,true);

                myChart.hideLoading();
            },
            error : function() {
                myChart.hideLoading();
            }
        });
			
    }

      //提示框
    function  tooltipFunc(params, ticket, callback,showType){
        var cate = params[1];

        var result='<table class="table-center">';
        result+='<tr><td>'+cate+'</td><td>';
       
        for(var key in comment)
        {	 
        	 if(cate==key ){
        	 	if(comment[key]=="high")
             		result += '</td><td>波峰</td>';
             	else
             		result += '</td><td>波谷</td>';
        	}
        }

        result += '</tr>';
		result+='<tr><td>'+params[2]+'</td></tr>';
        result+='</table>';
        return result;
    }   
    
    var zrnumber = require('echarts/util/number');
   
  	  function formatTime(time){
        return time.replace(/\-/g,'')
    }
    
    var zrUtil = require('zrender/tool/util');


    function getShowDays(){
        return compDataZoom_old.option.xAxis[0].data;
    }
    
    var compDataZoom_old;
    var legend_SelectedMap={};  //需要记忆图例的选择状态
    function hideDataZoom(){
        legend_SelectedMap=myChart.component.legend.getSelectedMap();
        compDataZoom_old=myChart.component.dataZoom;    //记忆缩放
        option_day.dataZoom.show=false;
        myChart.setOption(option_day,false);
    }

    function getIndexByName(data,name) {
        var dataLength = data.length;
        for (var i = 0; i < dataLength; i++) {
            if (data[i] == name || typeof data[i].value != 'undefined' && data[i].value == name) {
                return i;
            }
        }
        return -1;
    }
    function formatTime(time){
        return time;
    }

    function getAllDays(){
        return option_day.xAxis.data||option_day.xAxis[0].data;
    }
    
        $("#refresh_funnel_m_month_btn").click(function(){
            EC.show(businessName,2);
        });
        
    var EC={
        echart:myChart,
        'show':show,
        resize:function(){myChart.resize();},
        hideDataZoom:hideDataZoom,
        getShowDays:getShowDays,
        getAllDays:getAllDays
    };

    return EC;
});

